dojo.require("dojox.cometd");
dojo.require("dojox.cometd.timestamp");
$ = dojo.byId;

function empty(){}
var myconsole = $("console");

function reset() {
	$("console").innerHTML = "";
	dojox.cometd.publish("/desktop", "RESET");
}
function scrollOnUpdate() {
	var y = $("console").offsetHeight;
	scrollTo(0, y);
}

dojox.cometd.init("/console");
function start() {
	dojox.cometd.publish("/desktop", "START", {
		userAgent: window.navigator.userAgent,
		href: window.location.href
	});
	dojox.cometd.subscribe("/desktop", function(msg){
		if(typeof msg.data != "undefined") {
			switch(msg.data) {
				case "STOP":
					stop();
					break;
				case "ACK":
				case "RESET":
					break;
				default:
					if(typeof msg.data.userAgent != "undefined") {
						logUserAgent(msg.data.userAgent);
					}
					if(msg.data.newClient) {
						logMessage("New client connected from " + msg.data.href);
					}
					if(typeof msg.data.data != "undefined") {
						if(typeof msg.data.data.error != "undefined") {
							logError(msg.data.data.error);
						} else {
							logMessage(unescape(serialize(msg.data.data)));
						}
					}
					scrollOnUpdate();
			}
		}
	});
	logCommand("Console started...");
	
	$("startstop").setAttribute("value", "STOP");
	$("startstop").setAttribute("onclick", "stop()");
}
function stop() {
	logError("Console stopped!");
	dojox.cometd.unsubscribe("/desktop");
	dojox.cometd.publish("/desktop", "STOP");
	$("startstop").setAttribute("value", "START");
	$("startstop").setAttribute("onclick", "start()");
}
function logCommand(cmd) {
	var console = $("console");
	if(typeof console != "undefined") {
		console.innerHTML += '<div class="command-issued">&gt;&gt;&nbsp;' + cmd + '</iv>';
	}
}
function logMessage(msg) {
	var console = $("console");
	if(typeof console != "undefined") {
		console.innerHTML += '<div class="console-line">' + msg + '</div>';
	}
}
function logError(err) {
	var console = $("console");
	if(typeof console != "undefined") {
		console.innerHTML += '<div class="console-line error-line">' + err + '</div>';
	}
}
function logUserAgent(ua) {
	var console = $("console");
	if(typeof console != "undefined") {
		console.innerHTML += '<div class="user-agent">' + ua + '</div>';
	}
}

var commands = new Array();
commands.currentCommand = -1;
dojo.connect(document, "onkeyup", function(evt) {
	if(dojo.isSafari > 0) {
		dojo.keys.UP_ARROW = 38;
		dojo.keys.DOWN_ARROW = 40;
	}
	switch(evt.keyCode) {
		case dojo.keys.ENTER:
			var cmd = $("command-line").value;
			commands.push(cmd);
			commands.currentCommand = commands.length - 1;
			logCommand(cmd);
			scrollOnUpdate();
			dojox.cometd.publish("/mobile", cmd);
			$("command-line").value = "";
			break;
		case dojo.keys.UP_ARROW:
			if(commands.currentCommand > -1){
				$("command-line").value = commands[commands.currentCommand--]; 
			}
			break;
		case dojo.keys.DOWN_ARROW:
			if(commands.currentCommand < commands.length && commands.currentCommand > -1){
				$("command-line").value = commands[commands.currentCommand++]; 
			} else {
				$("command-line").value = "";
			}
			break;
	}
});

dojo.addOnLoad(start);
